// pages/my-order/my-order.js
import { Order } from '../../pages/order/order-model.js';

var order = new Order();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        pageIndex: [1, 1, 1, 1, 1],
        currentTabsIndex: 0,
        isLoadedAll: [false, false, false, false, false],
        loadingHidden: false,
        orderArr: [],
        orderUnPay: [],
        orderUnSend: [],
        orderUnGet: [],
        orderIsDone: [],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
		console.log(options)
		var status = options.statusFromMy
		var state = 0;
		if(status){
			switch (status){
				case '0' : state = 0;break;
				case '1': state = 1; break;
				case '2': state = 2; break;
				case '3': state = 3; break;
				case '4': state = 4; break;
			}
		}
		console.log(state)
		this.setData({
			currentTabsIndex: state
		})
        this._getOrders();
    },

    /*订单信息*/
    _getOrders: function (callback) {
        var that = this;
        var status = that.data.currentTabsIndex;
        order.getOrders(this.data.pageIndex[status], status, (res) => {
            var data = res.data;
            that.setData({
                loadingHidden: true
            });
            if (data.length > 0) {
                switch (status) {
                    case 0:
                        that.data.orderArr.push.apply(that.data.orderArr, res.data);  //数组合并
                        that.setData({
                            orderArr: that.data.orderArr
                        });
                        break;
                    case 1:
                        that.data.orderUnPay.push.apply(that.data.orderUnPay, res.data);  //数组合并
                        that.setData({
                            orderUnPay: that.data.orderUnPay
                        });
                        break;
                    case 2:
                        that.data.orderUnSend.push.apply(that.data.orderUnSend, res.data);  //数组合并
                        that.setData({
                            orderUnSend: that.data.orderUnSend
                        });
                        break;
                    case 3:
                        that.data.orderUnGet.push.apply(that.data.orderUnGet, res.data);  //数组合并
                        that.setData({
                            orderUnGet: that.data.orderUnGet
                        });
                        break;
                    case 4:
                        that.data.orderIsDone.push.apply(that.data.orderIsDone, res.data);  //数组合并
                        that.setData({
                            orderIsDone: that.data.orderIsDone
                        });
                        break;
                }
            } else {
                that.data.isLoadedAll[that.data.currentTabsIndex] = true;  //已经全部加载完毕
                that.data.pageIndex[that.data.currentTabsIndex] = 1;
            }
            callback && callback();
        });
    },

    //切换面板
    onTabsItemTap: function (event) {
        var index = order.getDataSet(event, 'index');
        this.setData({
            currentTabsIndex: index
        });
        switch (this.data.currentTabsIndex) {
            case 0: this.checkAndGetOrder(this.data.orderArr); break;
            case 1: this.checkAndGetOrder(this.data.orderUnPay); break;
            case 2: this.checkAndGetOrder(this.data.orderUnSend); break;
            case 3: this.checkAndGetOrder(this.data.orderUnGet); break;
            case 4: this.checkAndGetOrder(this.data.orderIsDone); break;
        }
    },
    checkAndGetOrder: function (or) {
        if (or.length <= 0) {
            this.setData({
                loadingHidden: false
            });
            this._getOrders();
        }
    },

    /*未支付订单再次支付*/
    rePay: function (event) {
        var id = order.getDataSet(event, 'id'),
            index = order.getDataSet(event, 'index');

        //online 上线实例，屏蔽支付功能
        if (order.onPay) {
            this._execPay(id, index);
        } else {
            this.showTips('支付提示', '本产品仅用于演示，支付系统已屏蔽');
        }
    },

    /*支付*/
    _execPay: function (id, index) {
        var that = this;
        order.execPay(id, (statusCode) => {
            if (statusCode > 0) {
                var flag = statusCode == 2;

                //更新订单显示状态
                if (flag) {
                    that.data.orderArr[index].status = 2;
                    that.setData({
                        orderArr: that.data.orderArr
                    });
                }

                //跳转到 成功页面
                wx.navigateTo({
                    url: '../pay-result/pay-result?id=' + id + '&flag=' + flag + '&from=my'
                });
            } else {
                that.showTips('支付失败', '商品已下架或库存不足');
            }
        });
    },

    /*显示订单的具体信息*/
    showOrderDetailInfo: function (event) {
        var id = order.getDataSet(event, 'id');
        wx.navigateTo({
            url: '../order/order?from=order&id=' + id
        });
    },

    onReachBottom: function () {
        if (!this.data.isLoadedAll[this.data.currentTabsIndex]) {
            this.data.pageIndex[this.data.currentTabsIndex]++;
            this._getOrders();
        }
    },

    /*下拉刷新页面*/
    onPullDownRefresh: function () {
        var that = this;
        // this.data.orderArr = [];  //订单初始化
        switch (this.data.currentTabsIndex) {
            case 0: this.data.orderArr = []; break;
            case 1: this.data.orderUnPay = []; break;
            case 2: this.data.orderUnSend = []; break;
            case 3: this.data.orderUnGet = []; break;
            case 4: this.data.orderIsDone = []; break;
        }
        this._getOrders(() => {
            that.data.isLoadedAll[this.data.currentTabsIndex] = false;  //是否加载完全
            that.data.pageIndex[this.data.currentTabsIndex] = 1;
            wx.stopPullDownRefresh();
            order.execSetStorageSync(false);  //更新标志位
        });
    },

	//分享效果
	onShareAppMessage: function () {
		var referral_code = wx.getStorageSync('referral_code');
		return {
			title: '唐潮盐匠-自贡冷吃系列',
			path: 'pages/home/home?referral_code=' + referral_code
		}
	}
})